package com.easy.goods.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.bean.Products;
import com.easy.common.CommonResult;
import com.easy.goods.service.ProductService;

@RestController
@RequestMapping("goods")
public class GoodsController {
	@Autowired
	private ProductService productService;

	@RequestMapping(method = RequestMethod.GET, value = "bycategoryid/{categoryid}")
	public CommonResult getProductBycategoryID(Page page, @PathVariable int categoryid) {

		List<Products> list = productService.getProductByCategoryID(page, categoryid);

		return CommonResult.success(list);

	}

	@RequestMapping(method = RequestMethod.GET, value = "getproductbycategory/{categoryid}/{count}")
	public CommonResult getProductByCategory(@PathVariable int categoryid,@PathVariable int count) {
		IPage page=new Page<>(1, count);
		List<Products> list = productService.getProductByCategoryID(page, categoryid);
		return CommonResult.success(list);
	}
	@RequestMapping(method = RequestMethod.GET, value = "/{id}")
	public CommonResult getProductByID(@PathVariable int id) {
		System.out.println("_-----------------"+id);
		Products product = productService.getProductByID(id);
		return CommonResult.success(product);
	}



	@RequestMapping(method = RequestMethod.GET, value = "goods")
	public String getGoods() {

		return "easygoods ----------8801";
	}

	@RequestMapping(method = RequestMethod.GET, value = "getproduct")
	public CommonResult getproduct(Products products) {
		List<Products> productsList = productService.list();
		return CommonResult.success(productsList);
	}

	@PostMapping("postproduct")
	public CommonResult postproduct(@RequestBody Products products) {
		Date date = new Date();
		date.getTime();
		products.setCreated(date);
		return CommonResult.success(productService.save(products));
	}

	@PutMapping("updateproduct")
	public CommonResult updateproduct(@RequestBody Products products) {
		return CommonResult.success(productService.updateById(products));
	}

	@DeleteMapping("deleteproduct/{id}")
	public CommonResult deleteproduct(@PathVariable Integer id) {

		return CommonResult.success(productService.deleteproduct(id));
//        return CommonResult.success(productService.deleteproduct(id));
	}

//@GetMapping("getcoreproduct")
//    public CommonResult getcoreproduct(@RequestBody Page ,Products products) {
//        Page<Products>  productsPage=new Page<>(page.getPage(),page.getSize());
//        return CommonResult.success(productService.page(page));
//    }
	@GetMapping("page")
	public CommonResult selectPage(Page page, Products products) {
		System.out.println(page.getSize() + "-----999" + products.getName() + "111------" + page.getCurrent());

		List list = productService.list(page, new QueryWrapper<Products>().like("name", products.getName()));
		long b = productService.count();
		System.out.println("总条数" + b);
		System.out.println("--------");
		int a = list.size();
		HashMap map = new HashMap();
		map.put("count", b);
		map.put("list", list);
		return CommonResult.success(map);
	}

	@PutMapping("shangxiajia/{id}")
	public void shangxiajia(@PathVariable Integer id) {
		productService.updateshangxiajia(id);
	}


}
